﻿namespace NHibernateCodeGenerator
{
    using DatabaseManage;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class Form1 : Form
    {
        private Button button1;
        private Button button2;
        private Button button3;
        private CheckBox checkBox1;
        private CheckBox checkBox2;
        private CheckedListBox checkedListBox1;
        private CheckedListBox checkedListBox2;
        private ComboBox comboBox1;
        private IContainer components = null;
        private AbsDatabaseInfo dbinfo = null;
        private GroupBox groupBox1;
        private GroupBox groupBox2;
        private GroupBox groupBox3;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private Label label6;
        private Label label7;
        private Label label8;
        private TabControl tabControl1;
        private TabPage tabPage1;
        private TabPage tabPage2;
        private TextBox textBox1;
        private TextBox textBox2;
        private TextBox textBox3;
        private TextBox textBox4;
        private TextBox textBox5;
        private TextBox textBox6;

        public Form1()
        {
            this.InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dbName = this.comboBox1.SelectedValue.ToString();
            string text = this.textBox2.Text;
            string str3 = this.textBox3.Text;
            string str4 = this.textBox5.Text;
            string str5 = this.textBox4.Text;
            if (string.IsNullOrEmpty(str3))
            {
                MessageBox.Show("Database Name Is Null!!");
            }
            else if (string.IsNullOrEmpty(text))
            {
                MessageBox.Show("Server Is Null!!");
            }
            else if (string.IsNullOrEmpty(str4))
            {
                MessageBox.Show("UserName Is Null!!");
            }
            else if (string.IsNullOrEmpty(str5))
            {
                MessageBox.Show("Password Is Null!!");
            }
            else
            {
                this.dbinfo = DatabaseFactory.CreateDatabase(dbName);
                if (this.dbinfo == null)
                {
                    MessageBox.Show("Create Instance Error!!");
                }
                else
                {
                    this.dbinfo.SetConnectionString(text, str3, str4, str5);
                    if (this.dbinfo.CheckDatabaseConnection())
                    {
                        this.label8.Visible = true;
                        this.checkedListBox1.DataSource = this.dbinfo.GetDatabaseAllTableNames();
                        this.checkedListBox1.DisplayMember = "TableName";
                        this.checkedListBox1.ValueMember = "TableName";
                        this.checkedListBox2.DataSource = this.dbinfo.GetDatabaseAllViewNames();
                        this.checkedListBox2.DisplayMember = "ViewName";
                        this.checkedListBox2.ValueMember = "ViewName";
                    }
                    else
                    {
                        MessageBox.Show("Connection Error!!");
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox6.Text = dialog.SelectedPath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string text = this.textBox1.Text;
            string str2 = this.textBox6.Text;
            if (string.IsNullOrEmpty(text))
            {
                MessageBox.Show("NameSpace Is Null!");
            }
            else if (string.IsNullOrEmpty(str2))
            {
                MessageBox.Show("Please Selected Save Path!");
            }
            else
            {
                WaitHelper helper = new WaitHelper();
                helper.WaitStart();
                List<string> selectedObjects = this.GetSelectedObjects(this.checkedListBox1);
                List<string> list2 = this.GetSelectedObjects(this.checkedListBox2);
                ClassStructFormat format = new ClassStructFormat();
                XmlStructFormat format2 = new XmlStructFormat();
                foreach (string str3 in selectedObjects)
                {
                    format.SetAttrbute(ObjectType.Table, str3, text, this.dbinfo);
                    format2.SetAttrbute(ObjectType.Table, str3, text, this.dbinfo);
                    NhbFileGenerator.CreateNhbClassFile(str2, format.CreateClassCode(), str3);
                    NhbFileGenerator.CreateNhbXmlFile(str2, format2.CreateXmlCode(), str3);
                }
                foreach (string str3 in list2)
                {
                    format.SetAttrbute(ObjectType.View, str3, text, this.dbinfo);
                    format2.SetAttrbute(ObjectType.View, str3, text, this.dbinfo);
                    NhbFileGenerator.CreateNhbClassFile(str2, format.CreateClassCode(), str3);
                    NhbFileGenerator.CreateNhbXmlFile(str2, format2.CreateXmlCode(), str3);
                }
                helper.WaitEnd();
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.SetCheckedListBox(this.checkedListBox1, this.checkBox1.Checked);
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            this.SetCheckedListBox(this.checkedListBox2, this.checkBox2.Checked);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.DataSource = DatabaseType.GetDbTypes();
            this.comboBox1.DisplayMember = "DisplayName";
            this.comboBox1.ValueMember = "ClassName";
        }

        private List<string> GetSelectedObjects(CheckedListBox chk)
        {
            List<string> list = new List<string>();
            int count = chk.Items.Count;
            for (int i = 0; i < count; i++)
            {
                if (chk.GetItemChecked(i))
                {
                    list.Add(((DataRowView) chk.Items[i]).Row[0].ToString());
                }
            }
            return list;
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(Form1));
            this.button1 = new Button();
            this.groupBox1 = new GroupBox();
            this.tabControl1 = new TabControl();
            this.tabPage1 = new TabPage();
            this.checkBox1 = new CheckBox();
            this.checkedListBox1 = new CheckedListBox();
            this.tabPage2 = new TabPage();
            this.checkBox2 = new CheckBox();
            this.checkedListBox2 = new CheckedListBox();
            this.textBox1 = new TextBox();
            this.label1 = new Label();
            this.groupBox2 = new GroupBox();
            this.label8 = new Label();
            this.textBox5 = new TextBox();
            this.label6 = new Label();
            this.textBox4 = new TextBox();
            this.label5 = new Label();
            this.textBox3 = new TextBox();
            this.label4 = new Label();
            this.textBox2 = new TextBox();
            this.label3 = new Label();
            this.comboBox1 = new ComboBox();
            this.label2 = new Label();
            this.groupBox3 = new GroupBox();
            this.button3 = new Button();
            this.button2 = new Button();
            this.textBox6 = new TextBox();
            this.label7 = new Label();
            this.groupBox1.SuspendLayout();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            base.SuspendLayout();
            this.button1.ForeColor = SystemColors.ControlText;
            this.button1.Location = new Point(0xcb, 0xc4);
            this.button1.Name = "button1";
            this.button1.Size = new Size(0x79, 0x17);
            this.button1.TabIndex = 0;
            this.button1.Text = "Connection";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new EventHandler(this.button1_Click);
            this.groupBox1.Controls.Add(this.tabControl1);
            this.groupBox1.Font = new Font("宋体", 10f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.groupBox1.ForeColor = SystemColors.Highlight;
            this.groupBox1.Location = new Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(0xed, 0x1a6);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Select Create Objects";
            this.tabControl1.Alignment = TabAlignment.Bottom;
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new Point(6, 0x18);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new Size(0xe1, 0x188);
            this.tabControl1.TabIndex = 0;
            this.tabPage1.Controls.Add(this.checkBox1);
            this.tabPage1.Controls.Add(this.checkedListBox1);
            this.tabPage1.Location = new Point(4, 4);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new Padding(3);
            this.tabPage1.Size = new Size(0xd9, 0x16e);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Tables";
            this.tabPage1.UseVisualStyleBackColor = true;
            this.checkBox1.AutoSize = true;
            this.checkBox1.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.checkBox1.Location = new Point(0x44, 0x153);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new Size(0x8f, 0x10);
            this.checkBox1.TabIndex = 1;
            this.checkBox1.Text = "Select All Tables";
            this.checkBox1.UseVisualStyleBackColor = true;
            this.checkBox1.CheckedChanged += new EventHandler(this.checkBox1_CheckedChanged);
            this.checkedListBox1.CheckOnClick = true;
            this.checkedListBox1.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.checkedListBox1.FormattingEnabled = true;
            this.checkedListBox1.Location = new Point(2, 5);
            this.checkedListBox1.Name = "checkedListBox1";
            this.checkedListBox1.Size = new Size(0xd3, 0x144);
            this.checkedListBox1.TabIndex = 0;
            this.tabPage2.Controls.Add(this.checkBox2);
            this.tabPage2.Controls.Add(this.checkedListBox2);
            this.tabPage2.Location = new Point(4, 4);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new Padding(3);
            this.tabPage2.Size = new Size(0xd9, 0x16e);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Views";
            this.tabPage2.UseVisualStyleBackColor = true;
            this.checkBox2.AutoSize = true;
            this.checkBox2.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.checkBox2.Location = new Point(0x44, 0x153);
            this.checkBox2.Name = "checkBox2";
            this.checkBox2.Size = new Size(0x88, 0x10);
            this.checkBox2.TabIndex = 3;
            this.checkBox2.Text = "Select All Views";
            this.checkBox2.UseVisualStyleBackColor = true;
            this.checkBox2.CheckedChanged += new EventHandler(this.checkBox2_CheckedChanged);
            this.checkedListBox2.CheckOnClick = true;
            this.checkedListBox2.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.checkedListBox2.FormattingEnabled = true;
            this.checkedListBox2.Location = new Point(2, 5);
            this.checkedListBox2.Name = "checkedListBox2";
            this.checkedListBox2.Size = new Size(0xd3, 0x144);
            this.checkedListBox2.TabIndex = 2;
            this.textBox1.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.textBox1.Location = new Point(0x58, 0x2f);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new Size(0xec, 0x15);
            this.textBox1.TabIndex = 2;
            this.textBox1.Text = "Model";
            this.label1.AutoSize = true;
            this.label1.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.label1.Location = new Point(7, 50);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x4b, 12);
            this.label1.TabIndex = 3;
            this.label1.Text = "NameSpace:";
            this.groupBox2.Controls.Add(this.label8);
            this.groupBox2.Controls.Add(this.textBox5);
            this.groupBox2.Controls.Add(this.label6);
            this.groupBox2.Controls.Add(this.textBox4);
            this.groupBox2.Controls.Add(this.label5);
            this.groupBox2.Controls.Add(this.button1);
            this.groupBox2.Controls.Add(this.textBox3);
            this.groupBox2.Controls.Add(this.label4);
            this.groupBox2.Controls.Add(this.textBox2);
            this.groupBox2.Controls.Add(this.label3);
            this.groupBox2.Controls.Add(this.comboBox1);
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Font = new Font("宋体", 11f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.groupBox2.ForeColor = SystemColors.Highlight;
            this.groupBox2.Location = new Point(0x106, 12);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new Size(330, 0xe5);
            this.groupBox2.TabIndex = 4;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Create Database Connection";
            this.label8.AutoSize = true;
            this.label8.Font = new Font("宋体", 10f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.label8.ForeColor = Color.Green;
            this.label8.Location = new Point(5, 200);
            this.label8.Name = "label8";
            this.label8.Size = new Size(0x97, 14);
            this.label8.TabIndex = 10;
            this.label8.Text = "Connection Succeed";
            this.label8.Visible = false;
            this.textBox5.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.textBox5.Location = new Point(80, 0x9d);
            this.textBox5.Name = "textBox5";
            this.textBox5.Size = new Size(0x3d, 0x15);
            this.textBox5.TabIndex = 9;
            this.label6.AutoSize = true;
            this.label6.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.label6.Location = new Point(0x94, 0xa3);
            this.label6.Name = "label6";
            this.label6.Size = new Size(0x44, 12);
            this.label6.TabIndex = 8;
            this.label6.Text = "Password:";
            this.textBox4.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.textBox4.Location = new Point(0xdd, 0x9d);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new Size(0x67, 0x15);
            this.textBox4.TabIndex = 7;
            this.label5.AutoSize = true;
            this.label5.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.label5.Location = new Point(6, 0xa3);
            this.label5.Name = "label5";
            this.label5.Size = new Size(40, 12);
            this.label5.TabIndex = 6;
            this.label5.Text = "User:";
            this.textBox3.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.textBox3.Location = new Point(0x81, 0x4c);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new Size(0xc3, 0x15);
            this.textBox3.TabIndex = 5;
            this.label4.AutoSize = true;
            this.label4.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.label4.Location = new Point(6, 0x52);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x67, 12);
            this.label4.TabIndex = 4;
            this.label4.Text = "Database Name:";
            this.textBox2.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.textBox2.Location = new Point(80, 0x73);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new Size(0xf4, 0x15);
            this.textBox2.TabIndex = 3;
            this.label3.AutoSize = true;
            this.label3.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.label3.Location = new Point(6, 0x79);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x36, 12);
            this.label3.TabIndex = 2;
            this.label3.Text = "Server:";
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            this.comboBox1.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new Point(0x81, 0x25);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new Size(0xc3, 20);
            this.comboBox1.TabIndex = 1;
            this.label2.AutoSize = true;
            this.label2.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.label2.Location = new Point(6, 40);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x67, 12);
            this.label2.TabIndex = 0;
            this.label2.Text = "Database Type:";
            this.groupBox3.Controls.Add(this.button3);
            this.groupBox3.Controls.Add(this.button2);
            this.groupBox3.Controls.Add(this.textBox6);
            this.groupBox3.Controls.Add(this.label7);
            this.groupBox3.Controls.Add(this.textBox1);
            this.groupBox3.Controls.Add(this.label1);
            this.groupBox3.Font = new Font("宋体", 11f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.groupBox3.ForeColor = SystemColors.Highlight;
            this.groupBox3.Location = new Point(0x106, 0xf7);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new Size(330, 0xbb);
            this.groupBox3.TabIndex = 5;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Code Generator";
            this.button3.ForeColor = SystemColors.ControlText;
            this.button3.Location = new Point(0xcb, 0x91);
            this.button3.Name = "button3";
            this.button3.Size = new Size(0x79, 0x17);
            this.button3.TabIndex = 7;
            this.button3.Text = "Create Code";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new EventHandler(this.button3_Click);
            this.button2.ForeColor = SystemColors.Highlight;
            this.button2.Location = new Point(0xfe, 0x5f);
            this.button2.Name = "button2";
            this.button2.Size = new Size(70, 0x17);
            this.button2.TabIndex = 6;
            this.button2.Text = "Select";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new EventHandler(this.button2_Click);
            this.textBox6.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.textBox6.Location = new Point(0x58, 0x60);
            this.textBox6.Name = "textBox6";
            this.textBox6.ReadOnly = true;
            this.textBox6.Size = new Size(160, 0x15);
            this.textBox6.TabIndex = 4;
            this.label7.AutoSize = true;
            this.label7.Font = new Font("宋体", 9f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.label7.Location = new Point(7, 0x63);
            this.label7.Name = "label7";
            this.label7.Size = new Size(0x44, 12);
            this.label7.TabIndex = 5;
            this.label7.Text = "SavePath:";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            base.ClientSize = new Size(0x25c, 0x1be);
            base.Controls.Add(this.groupBox3);
            base.Controls.Add(this.groupBox2);
            base.Controls.Add(this.groupBox1);
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            this.MaximumSize = new Size(0x264, 480);
            base.MinimizeBox = false;
            this.MinimumSize = new Size(0x264, 480);
            base.Name = "Form1";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "NHibernateCodeGenerator";
            base.Load += new EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            this.tabPage2.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            base.ResumeLayout(false);
        }

        private void SetCheckedListBox(CheckedListBox chk, bool state)
        {
            int count = chk.Items.Count;
            for (int i = 0; i < count; i++)
            {
                chk.SetItemChecked(i, state);
            }
        }
    }
}

